﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_MyUserControls_TH_LoaiSanPhamAdmin : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Load the grid only the first time the page is loaded
        if (!Page.IsPostBack)
        {
            // Load the categories grid
            BindGrid();            
        }
    }

    // Populate the GridView with data
    private void BindGrid()
    {
        LoaiSanPhamBUS objLoaiSP = new LoaiSanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objLoaiSP.SelectLoaiSanPhamAll();
        grid.DataSource = objDataSet.Tables["SelectLoaiSanPhamAll"];
        grid.DataBind();
    }

    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Set the row for which to enable edit mode
          grid.EditIndex = e.NewEditIndex; 
          // Set status message 
          // Reload the grid
          BindGrid();
    }
    protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        // Cancel edit mode
        grid.EditIndex = -1;
        // Set status message
        // Reload the grid
        BindGrid();
    }
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // Retrieve updated data
        string id = grid.DataKeys[e.RowIndex].Value.ToString();
        string name = ((TextBox)grid.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        string description = ((TextBox)grid.Rows[e.RowIndex].
              FindControl("txtMoTa")).Text;
        string visible = ((CheckBox)grid.Rows[e.RowIndex].Cells[2].
              Controls[0]).Checked.ToString();

        LoaiSanPhamBUS objLoaiSanPham = new LoaiSanPhamBUS(TGDDConfiguration.DbConnectionString);

        LoaiSanPhamDTO categoryDto = new LoaiSanPhamDTO();
        categoryDto.Id = int.Parse(id);
        categoryDto.TenLoaiSP = name;
        categoryDto.MoTa = description;
        categoryDto.Visible = true;

        try
        {
            // Execute the update command
            bool success = objLoaiSanPham.UpdateLoaiSanPham(categoryDto);
            // Cancel edit mode
            grid.EditIndex = -1;
            // Display status message
            lblStatus.Text = success ? "Cập nhật thành công" : "Cập nhật thất bại";
            // Reload the grid
            BindGrid();
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Get the ID of the record to be deleted
        string id = grid.DataKeys[e.RowIndex].Value.ToString();
        LoaiSanPhamBUS objLoaiSanPham = new LoaiSanPhamBUS(TGDDConfiguration.DbConnectionString); 
        try
        {
            // Execute the delete command
            bool success = objLoaiSanPham.DeleteLoaiSanPham(int.Parse(id));
            // Cancel edit mode
            grid.EditIndex = -1;
            // Display status message
            lblStatus.Text = success ? "Xóa thành công" : "Xóa thất bại";
            // Reload the grid
            BindGrid();
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkbtnDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Bạn có chắc chắn muốn xóa không?' )");
        }
    }
}
